package com.example.module_motorcircle.dialog

import android.content.Context
import android.os.Bundle
import android.util.DisplayMetrics
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import android.view.inputmethod.InputMethodManager
import androidx.core.os.HandlerCompat.postDelayed
import androidx.core.widget.addTextChangedListener
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.FragmentManager
import com.example.module_motorcircle.R
import com.example.module_motorcircle.databinding.DialogCommentBinding
import com.example.module_motorcircle.databinding.DialogMoreActionBinding
import com.example.module_motorcircle.dialog.MoreActionDialog.Companion

/**
 * @author yaotianxue
 * @description
 * @date 2025/2/17
 */
class CommentDialog(
    private val hint:String? = null,
    private val onClickSend: ((String) -> Unit)? = null
): DialogFragment() {
    private lateinit var mParentView: View
    private lateinit var mBinding:DialogCommentBinding

    companion object {
        val TAG: String = MoreActionDialog::class.java.simpleName
    }
    //创建
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setStyle(STYLE_NO_TITLE, R.style.MoreActionDialog)
    }

    /**
     * 调整对话框的宽高和位置
     */
    override fun onStart() {
        super.onStart()
        //获得对话框的属性
        val window = dialog?.window
        val attributes = window?.attributes
        //获得窗口的宽度和高度
        val dm = DisplayMetrics()
        activity?.windowManager?.defaultDisplay?.getMetrics(dm)
        window?.clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE)
        //修改对话框的属性并重新设置给当前对话框
        attributes?.width = dm.widthPixels
        attributes?.gravity = Gravity.BOTTOM
        window?.attributes = attributes
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        mBinding = DialogCommentBinding.inflate(inflater,container,false)
        mParentView = mBinding.root
        return mParentView
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        mBinding.apply {
            //点击发送
            tvSend.setOnClickListener {
                onClickSend?.invoke(etComments.text.toString())
            }
            //是否可点击
            etComments.addTextChangedListener {
                tvSend.isEnabled = !it.isNullOrEmpty()
            }
            //默认显示内容
            if(hint != null){
                etComments.hint = hint
            }


        }
        //显示键盘
        showSoftKeyBoard()

    }
    private fun showSoftKeyBoard(){
        val manager = context?.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        mBinding.etComments.postDelayed({
            mBinding.etComments.requestFocus()
            manager.showSoftInput(mBinding.etComments,InputMethodManager.RESULT_UNCHANGED_SHOWN)
        },200)
    }

    private fun hideSoftKeyBoard(){
        val manager = context?.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        manager.hideSoftInputFromWindow(mBinding.etComments.windowToken,0)
    }


    /**
     * 打开对话框
     */
    fun show(fragmentManager: FragmentManager){
        if(fragmentManager.findFragmentByTag(MoreActionDialog.TAG)?.isVisible === true){
            return
        }
        this.show(fragmentManager, MoreActionDialog.TAG)
    }
    /**
     * 关闭对话框
     */
    override fun dismiss(){
        mBinding.etComments.clearFocus()
        hideSoftKeyBoard()
        super.dismiss()

    }

}